package com.mengma.ec.controller.portal;

import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.ICartService;
import com.mengma.ec.vo.CartProductVo;
import com.mengma.ec.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: xixi
 * @Date: 2018/9/20 09:17
 * @Description: 购物车接口
 */

@RequestMapping("/cart/")
@RestController
public class CartController {

    @Autowired
    private ICartService iCartService;

    //1.购物车List列表
    @GetMapping("list.do")
    public ServerResponse<CartVo> list(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse<CartVo> response = iCartService.findAllList(user.getId());
        return response;
    }

    //2.购物车添加商品
    @PostMapping("add.do")
    public ServerResponse<CartVo> add(HttpSession session,
                                      @RequestParam(required = true )Integer productId,
                                      @RequestParam(required = true )Integer count){
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse<CartVo> response = iCartService.add(user.getId(),productId,count);
        return response;
    }

    //3.更新购物车某个产品数量
    @PostMapping("updata.do")
    public ServerResponse<CartVo> updata(HttpSession session,
                                      @RequestParam(required = true )Integer productId,
                                      @RequestParam(required = true )Integer count){
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse<CartVo> response = iCartService.updata(user.getId(),productId,count);
        return response;
    }

    //4.移除购物车某个产品
    @PostMapping("delete.do")
    public ServerResponse<CartVo> delete(HttpSession session,String productIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse<CartVo> response = iCartService.delete(user.getId(),productIds);
        
        return response;


    }




}
